<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of session
 *
 * @author jpsiffert
 */
class session {
  
  private static $_instance = null;
  
  /**
   *
   * @param string $name
   * @return bool 
   */
  public function __isset($name) {
    $ret = $this->get($name);
    return ($ret!=null);
  }
  
  function _set_get_unset($mode,$name,$value=null){
    $ret=null;
    switch($mode){
      case 'set':
        if(!isset($_SESSION['lolita'])){
          $_SESSION['lolita'] = array();
        }
        break;
    }
    if(isset($_SESSION['lolita'])){
      $root = &$_SESSION['lolita']; 
      $tab  = explode('/', $name);
      for($i=0;$i<count($tab);$i+=1){
        $arg = $tab[$i];
        
        switch($mode){
        case 'set':
          if(!isset($root[$arg])){
            $root[$arg]='';
          } 
          $root = &$root[$arg];
          $ret  = &$root;
          $ret  = $value;
          break;
        case 'unset':
          if(isset($root[$arg])){
            $ret  = $root[$arg];
            unset($root[$arg]);
          } 
          break;
        case 'get':
          if(isset($root[$arg])){
            $root = &$root[$arg];
            $ret  = &$root;
          }
          break;
        }
      }
    }
    return $ret;
  }
  
  public function __unset($name) {
    $this->_set_get_unset('unset',$name);
  }
  
  public function __get($name) {
    $ret = $this->_set_get_unset('get', $name);
//    $ret=null;
//    if(isset($_SESSION['lolita'])){
//      $root = $_SESSION['lolita']; 
//      $tab  = explode('/', $name);
//      for($i=0;$i<count($tab);$i+=1){
//        $value = $tab[$i];
//        if(isset($root[$value])){
//          $root = $root[$value];
//          $ret  = $root;
//        } else {
//          $ret=null;
//          break;
//        }
//      }
//    } 
    
    return $ret;
  }
  
  public function __set($name,$value) {
    $this->_set_get_unset('set', $name, $value);
//    $ret=null;
//    if(!isset($_SESSION['lolita'])){
//      $_SESSION['lolita']=array();
//    }
//
//    $root = &$_SESSION['lolita']; 
//    $tab  = explode('/', $name);
//    for($i=0;$i<count($tab);$i+=1){
//      $val = $tab[$i];
//      if(!isset($root[$val])){
//        $root[$val]='';
//      } 
//      $root = &$root[$val];
//      $ret  = &$root;
//    }
//     
//    $ret = $value;
    
    return $this;
  }
  
  /**
   * @return session 
   */
  protected static function _getInstance(){
    if(is_null(self::$_instance)){
      self::$_instance = new session();
    }
    return self::$_instance;
  }

  /**
   * @param string $name
   * @return mixed 
   */
  public static function get($name){
    $session = self::_getInstance();
    $ret = $session->$name;
    return $ret;
  }

  /**
   * @param string $name
   * @param mixed $value
   * @return session 
   */
  public static function set($name,$value){
    $session = self::_getInstance();
    $session->$name = $value;
    return $session;
  }

  /**
   * @param string $name
   * @return session 
   */
  public static function destroy(){
    $session = self::_getInstance();
    
    foreach(func_get_args() as $key=>$value){
      unset($session->$value);
    }
    
    return $session;
  }
  
  /**
   * @param string $name
   * @param mixed $value
   * @return session 
   */
  public static function setIfNull($name,$value){
    $session = self::_getInstance();
    if(!isset($session->$name) && is_null($session->$name) ){
      $session->$name = $value;
    }
    return $session;
  }
  
  
}

